package com.xbdun.service.bmp.facade;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xbdun.common.api.Client;
import com.xbdun.common.api.XbdUnStringResult;
import com.xbdun.common.enums.YesOrNoEnum;
import com.xbdun.common.exceptions.BizException;
import com.xbdun.common.util.CommonUtil;
import com.xbdun.facade.bmp.enumx.SysMenuEnum.SysMenuBldelEnum;
import com.xbdun.facade.bmp.enumx.SysMenuEnum.SysMenuDisplayEnum;
import com.xbdun.facade.bmp.enumx.SysMenuEnum.SysMenuEnableEnum;
import com.xbdun.facade.bmp.enumx.SysMenuEnum.SysMenuTargetEnum;
import com.xbdun.facade.bmp.request.system.SysMenuRequest;
import com.xbdun.facade.bmp.response.system.SysMenuResponse;
import com.xbdun.facade.bmp.result.system.MenuPlatResult;
import com.xbdun.facade.bmp.result.system.PlatListResult;
import com.xbdun.facade.bmp.result.system.SysMenuResult;
import com.xbdun.facade.bmp.result.system.TreeNodeResult;
import com.xbdun.facade.bmp.service.system.SysMenuFacade;
import com.xbdun.service.bmp.service.system.SysMenuService;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: SysMenuFacadeImpl
 * @Description: 平台菜单信息
 * @author zhanggt 360730440@qq.com
 * @date 2017年3月21日 下午3:15:30
 */
@Component
@Slf4j
public class SysMenuFacadeImpl implements SysMenuFacade {
	
	@Autowired
	private SysMenuService sysMenuService;

	/**
	 * @Title: addSysMenu
	 * @Description: 新增平台菜单信息
	 * @param request 请求参数
	 * @param client 客户端
	 * @throws Exception  设定文件
	 * @return void 返回类型
	 */
	public XbdUnStringResult addSysMenu(SysMenuRequest request, Client client) {
		XbdUnStringResult result = new XbdUnStringResult();
		try {
			log.info("start - 新增平台菜单初始参数,{}", request);
			CommonUtil.validateClient(client);
			addSysMenuChecked(request);
			// 设置菜单默认值
			request.setBlDel(SysMenuBldelEnum.DELETE_NO.getKey());
			request.setBlPlat(YesOrNoEnum.No.getKey());
			sysMenuService.addSysMenu(request);
		} catch (Exception ex) {
			result.setBizException(ex);
			log.error("新增平台菜单失败,入参:{},异常:{}", request, ex);
		} finally {
			log.info("end - 新增平台菜单结果:{}", result);
		}
		return result;
	}

	// 检测平台菜单信息
	public void addSysMenuChecked(SysMenuRequest request) {
		if (null == request){
			throw BizException.PARAM_CHKE_FAIL.newInstance("请求对象不能为空");
		}
		StringBuilder sb = new StringBuilder();
		if(request.getCode()>0&&null!=request.getPlatCode() && request.getPlatCode().intValue()>0){
			sb.append("菜单Code["+request.getCode()+"],平台Code["+request.getPlatCode()+"];");
		}
		if(request.getPlatCode()==null) {
			if (request.getCode() < 0) {
				sb.append("菜单code不能为负;");
			}
			if (StringUtils.isBlank(request.getName())) {
				sb.append("菜单名称不能为空;");
			}
			request.setTarget(StringUtils.defaultIfBlank(request.getTarget(), SysMenuTargetEnum._SELF.getVal()));
			if (!SysMenuTargetEnum.isValue(request.getTarget())) {
				sb.append("菜单的打开方式有误;");
			}
			if (null == SysMenuEnableEnum.getEnum(request.getEnable())) {
				sb.append("菜单的启用未定义;");
			}
			request.setBlDisplay(null==request.getBlDisplay()?Byte.valueOf(SysMenuDisplayEnum.PLAY.getKey()+""):request.getBlDisplay());
			if (null == SysMenuDisplayEnum.getEnum(request.getBlDisplay())) {
				sb.append("菜单的显示方式未定义;");
			}
		}else{
			if(request.getPlatCode().longValue()<0){
				sb.append("平台编码不能小于0");
			}
		}
		if (sb.length() > 0) {
			throw BizException.PARAM_CHKE_FAIL.newInstance("%s", sb.toString());
		}
	}

	/**
	* @Title: getSysMenu
	* @Description: 根据用户ID查询用户基本信息
	* @param id
	* @throws Exception    设定文件
	* @return SysMenuResult    返回类型
	 */
	public SysMenuResult getSysMenu(long id){
		SysMenuResult result = new SysMenuResult();
		try{
			SysMenuResponse menuInfo = sysMenuService.getSysMenu(id);
			result.setData(menuInfo);
		}catch(Exception e){
			result.setBizException(e);
			log.error("获取企业基本信息失败,入参:{},异常:{}",id,e);
		}
		return result;
	}

	/**
	* @Title: modifySysMenu
	* @Description: 修改菜单信息
	* @param request	请求参数
	* @return XbdUnStringResult    返回类型
	 */
	public XbdUnStringResult modifySysMenu(SysMenuRequest request, Client client){
		XbdUnStringResult result = new XbdUnStringResult();
		try{
			log.info("start - 修改菜单信息入参:" + request);
			CommonUtil.validateClient(client);
			modifySysMenuChecked(request);
			sysMenuService.modifySysMenu(request);	
		}catch(Exception e){
			result.setBizException(e);
			log.error("修改菜单信息失败,入参:{},异常:{}",request,e);
		}finally{
			log.info("end - 修改菜单信息入参{}",result);
		}
		return result;
	}

	private void modifySysMenuChecked(SysMenuRequest request) {
		if (null == request){
			throw BizException.PARAM_CHKE_FAIL.newInstance("请求对象不能为空");
		}
		StringBuilder sb = new StringBuilder();
		if (request.getCode() < 0) {
			sb.append("菜单code不能为负;");
		}else if (request.getCode() < 1000) {
			sb.append("平台菜单不能修改;");
		}
		if(StringUtils.isBlank(request.getName())){
			sb.append("菜单名称不能为空;");
		}
		if(StringUtils.isBlank(request.getHit())){
			sb.append("菜单Title不能为空;");
		}
		request.setTarget(StringUtils.defaultIfBlank(request.getTarget(), SysMenuTargetEnum._SELF.getVal()));
		if (!SysMenuTargetEnum.isValue(request.getTarget())) {
			sb.append("菜单的打开方式有误;");
		}
		if (null == SysMenuEnableEnum.getEnum(request.getEnable())) {
			sb.append("菜单的启用未定义;");
		}
		if (null!=request.getBlDisplay()) {
			if(null == SysMenuDisplayEnum.getEnum(request.getBlDisplay())){
				sb.append("菜单的显示方式未定义;");
			}
		}
		if (sb.length() > 0) {
			throw BizException.PARAM_CHKE_FAIL.newInstance("%s", sb.toString());
		}
	}

	/**
	* @Title: queryTreeMenu
	* @Description: 查询菜单树信息
	* @param orgCode	当前企业Code
	* @param client	客户端
	* @return XbdUnStringResult    返回类型
	 */
	public TreeNodeResult queryGrantMenu(Long orgCode,Client client) {
		TreeNodeResult result = new TreeNodeResult();
		try{
			log.info("start - 获取菜单初始参数,{}", client);
			
			CommonUtil.validateClient(client);
			if (null == orgCode || orgCode.longValue() < 0) {
				throw BizException.PARAM_CHKE_FAIL.newInstance("企业编码不合法");
			}
			result.setData(sysMenuService.queryGrantMenu(orgCode,client));
		} catch (Exception ex) {
			result.setBizException(ex);
			log.error("获取菜单失败,入参:{},异常:{}", client, ex);
		} finally {
			log.info("end - 获取菜单结果:{}", result);
		}
		return result;
	}

	public TreeNodeResult queryMenuTree(long code, Client client) {
		TreeNodeResult result = new TreeNodeResult();
		try{
			log.info("start - 获取菜单初始参数,{}", client);
			CommonUtil.validateClient(client);
			if(code<0||code>999) {
				throw BizException.PARAM_CHKE_FAIL.newInstance("传入的平台编号有误!");
			}
			result.setData(sysMenuService.queryMenuTree(code,client));
		} catch (Exception ex) {
			result.setBizException(ex);
			log.error("获取菜单失败,入参:{},异常:{}", client, ex);
		} finally {
			log.info("end - 获取菜单结果:{}", result);
		}
		return result;
	}

	public MenuPlatResult getAllPlatByMenu(Client client) {
		MenuPlatResult result = new MenuPlatResult();
		try{
			log.info("start - 获取菜单中所有属于平台的菜单初始参数,{}", client);
			CommonUtil.validateClient(client);
			result.setData(sysMenuService.getAllPlatByMenu(client));
		} catch (Exception ex) {
			result.setBizException(ex);
			log.error("获取菜单中所有属于平台的菜单失败,入参:{},异常:{}", client, ex);
		} finally {
			log.info("end - 获取菜单中所有属于平台的菜单结果:{}", result);
		}
		return result;
	}

	public PlatListResult getAllPlat(Client client) {
		PlatListResult result = new PlatListResult();
		try{
			log.info("start - 获取所有平台初始参数,{}", client);
			CommonUtil.validateClient(client);
			result.setData(sysMenuService.getAllPlat(client));
		} catch (Exception ex) {
			result.setBizException(ex);
			log.error("获取所有平台失败,入参:{},异常:{}", client, ex);
		} finally {
			log.info("end - 获取所有平台结果:{}", result);
		}
		return result;
	}

	public SysMenuResult getMenuInfo(Long code, Client client) {
		SysMenuResult result = new SysMenuResult();
		try{
			if (null==code||code.longValue()<0){
				throw BizException.PARAM_CHKE_FAIL.newInstance("菜单编号有误!");
			}
			log.info("start - 获取菜单详情初始参数,{}", client);
			CommonUtil.validateClient(client);
			result.setData(sysMenuService.getMenuInfo(code,client));
		} catch (Exception ex) {
			result.setBizException(ex);
			log.error("获取菜单详情失败,入参:{},异常:{}", client, ex);
		} finally {
			log.info("end - 获取菜单详情结果:{}", result);
		}
		return result;
	}

	public XbdUnStringResult deleteSysMenu(Long code, Client client) {
		XbdUnStringResult result = new XbdUnStringResult();
		try{
			log.info("start - 删除菜单信息入参:" + code);
			CommonUtil.validateClient(client);
			if(null == code){
				throw BizException.PARAM_CHKE_FAIL.newInstance("请求对象为空;");
			}
			if(code.longValue()<0) {
				throw BizException.PARAM_CHKE_FAIL.newInstance("删除的菜单code不能为负");
			}
			sysMenuService.deleteSysMenu(code);	
		}catch(Exception e){
			result.setBizException(e);
			log.error("删除菜单信息失败,入参:{},异常:{}",code,e);
		}finally{
			log.info("end - 删除菜单信息入参{}",result);
		}
		return result;
	}
}
